package com.sanqing.main;

import com.sanqing.iframe.*;
import com.sanqing.model.Operater;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class Library extends JFrame {
    private static final JDesktopPane DESKTOP_PANE = new JDesktopPane();   //桌面窗体
    private final Operater operater;

    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());      //设置系统界面外观
            new BookLoginIFrame();  //登录窗口
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public static void addIFrame(JInternalFrame iframe) {        //添加子窗体的方法
        DESKTOP_PANE.add(iframe);       //新增子窗体
    }

    public Library(Operater operater) {
        super();

        Toolkit tk = Toolkit.getDefaultToolkit();
        java.awt.Image img = tk.getImage("images/icon.png");
        setIconImage(img);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);        //关闭按钮处理事件
        Toolkit tool = Toolkit.getDefaultToolkit();                     //获得默认的工具箱
        Dimension screenSize = tool.getScreenSize();                    //获得屏幕的大小
        setSize(1600, 800);
        setLocation((screenSize.width - getWidth()) / 2,
                (screenSize.height - getHeight()) / 2);                     //设置窗体位置
        setTitle("图书馆管理系统");                                        //设置窗体标题
        JMenuBar menuBar = createMenu();                                //调用创建菜单栏的方法
        setJMenuBar(menuBar);                                           //设置菜单栏

        JToolBar toolBar = createToolBar();
        getContentPane().add(toolBar, BorderLayout.NORTH);               //设置工具栏
//      getContentPane().add(getJPanel(),BorderLayout.CENTER);


        /*final JLabel label = new JLabel();                              //创建一个标签，用来显示图片
        label.setBounds(0, 0, 0, 0);                                       //设置窗体的大小和位置
        label.setIcon(null);                                            //窗体背景
        DESKTOP_PANE.addComponentListener(new ComponentAdapter() {
            public void componentResized(final ComponentEvent e) {
                Dimension size = e.getComponent().getSize();            //获得组件大小
                label.setSize(e.getComponent().getSize());              //设置标签大小
                label.setText("<html><img width=" + size.width + "height="
                        + size.height + "src="
                        + this.getClass().getResource("/backImg.jpg")
                        + "></html>");       //设置标签文本
            }
        });
        DESKTOP_PANE.add(label, new Integer(Integer.MIN_VALUE));*/         //添加到桌面窗体
        getContentPane().add(DESKTOP_PANE);//将桌面窗体添加到主窗体中
        this.operater = operater;
        if (operater.getGrade() == 1) {              //如果是操作员，就不显示图书，订阅和员工管理界面
            setAdmin();
        }
    }

    public void setAdmin() {
        JToolBar jToolBar = (JToolBar) this.getContentPane().getComponent(0);
        jToolBar.getComponent(4).setVisible(false);
        jToolBar.getComponent(5).setVisible(false);
        getJMenuBar().getMenu(2).setVisible(false);
        getJMenuBar().getMenu(3).setVisible(false);
        getJMenuBar().getMenu(4).setVisible(false);
    }

    private JMenuBar createMenu() {  //创建菜单栏的方法
        /*
         * 主窗体菜单栏设计
         * */
        JMenuBar menuBar = new JMenuBar();   //创建菜单栏

        JMenu baseMenu1 = new JMenu("借阅"); // 借阅菜单
        baseMenu1.setFont(new Font("宋体", Font.PLAIN, 20));
        baseMenu1.setIcon(new ImageIcon("images/bookIFrame.png"));
        baseMenu1.setMnemonic('b');
        JMenuItem borrowItem = new JMenuItem("借书");       // 子菜单
        borrowItem.addActionListener(new borrowBookMen());
        borrowItem.setFont(new Font("宋体", Font.PLAIN, 20));
        JMenuItem backItem = new JMenuItem("还书");
        backItem.addActionListener(new backItemAction());
        backItem.setFont(new Font("宋体", Font.PLAIN, 20));
        JMenuItem setborrowItem = new JMenuItem("设置");
        setborrowItem.addActionListener(new setborrowItemAction());
        setborrowItem.setFont(new Font("宋体", Font.PLAIN, 20));
        borrowItem.setIcon(new ImageIcon("images/borrow.png"));  // 设置图标
        backItem.setIcon(new ImageIcon("images/back.png"));
        setborrowItem.setIcon(new ImageIcon("images/settings.png"));
        baseMenu1.add(borrowItem);   // 添加到借阅菜单
        baseMenu1.add(backItem);
        baseMenu1.add(setborrowItem);

        JMenu baseMenu2 = new JMenu("读者");               //读者信息菜单菜单
        baseMenu2.setFont(new Font("宋体", Font.PLAIN, 20));
        baseMenu2.setIcon(new ImageIcon("images/reader.png"));       //设置菜单图标
        baseMenu2.setMnemonic('R');                  //添加快捷键 r

        JMenuItem readerManagerMItem = new JMenuItem("添加新的读者");     //读者信息管理子菜单
        readerManagerMItem.setFont(new Font("宋体", Font.PLAIN, 20));
        readerManagerMItem.addActionListener(new AddNewReader());
        JMenuItem readerManagerMItem2 = new JMenuItem("修改读者信息");
        readerManagerMItem2.setFont(new Font("宋体", Font.PLAIN, 20));
        readerManagerMItem2.addActionListener(new EditReaderAction());
        JMenuItem readerManagerMItem3 = new JMenuItem("查询读者信息");
        readerManagerMItem3.setFont(new Font("宋体", Font.PLAIN, 20));
        readerManagerMItem3.addActionListener(new showReaderAction());
        readerManagerMItem.setIcon(new ImageIcon("images/add.png"));
        readerManagerMItem2.setIcon(new ImageIcon("images/edit.png"));
        readerManagerMItem3.setIcon(new ImageIcon("images/search.png"));
        baseMenu2.add(readerManagerMItem);
        baseMenu2.add(readerManagerMItem2);
        baseMenu2.add(readerManagerMItem3);

        JMenu baseMenu3 = new JMenu("图书");    // 图书管理菜单
        baseMenu3.setFont(new Font("宋体", Font.PLAIN, 20));
        baseMenu3.setIcon(new ImageIcon("images/book1.png"));
        JMenuItem addBook = new JMenuItem("添加图书");
        addBook.setFont(new Font("宋体", Font.PLAIN, 20));
        addBook.addActionListener(new BookAddBut());
        JMenuItem editBook = new JMenuItem("修改图书");
        editBook.setFont(new Font("宋体", Font.PLAIN, 20));
        editBook.addActionListener(new BookEditBut());
        JMenuItem searchBook = new JMenuItem("浏览图书");
        searchBook.setFont(new Font("宋体", Font.PLAIN, 20));
        searchBook.addActionListener(new searchAction());
        JMenuItem xiajiaItem = new JMenuItem("下架图书");
        xiajiaItem.setFont(new Font("宋体", Font.PLAIN, 20));
        xiajiaItem.addActionListener(new xiajiaItemAction());
        addBook.setIcon(new ImageIcon("images/add.png"));
        editBook.setIcon(new ImageIcon("images/edit.png"));
        searchBook.setIcon(new ImageIcon("images/book_search.png"));
        baseMenu3.add(addBook);
        baseMenu3.add(editBook);
        baseMenu3.add(searchBook);
        baseMenu3.add(xiajiaItem);

        JMenu baseMenu4 = new JMenu("订阅");                // 订阅图书菜单
        baseMenu4.setFont(new Font("宋体", Font.PLAIN, 20));
        baseMenu4.setIcon(new ImageIcon("images/order1.png"));

        JMenuItem addOrder = new JMenuItem("添加新订单");
        addOrder.setFont(new Font("宋体", Font.PLAIN, 20));
        addOrder.addActionListener(new BookOrderBut());

        JMenuItem editOrder = new JMenuItem("修改订单");
        editOrder.setFont(new Font("宋体", Font.PLAIN, 20));
        editOrder.addActionListener(new BookOrderEditBut());

        JMenuItem allOrder = new JMenuItem("查询订单");
        allOrder.setFont(new Font("宋体", Font.PLAIN, 20));
        allOrder.addActionListener(new BookSearchOrderBut());

        JMenuItem inputOrder = new JMenuItem("图书入库");
        inputOrder.setFont(new Font("宋体", Font.PLAIN, 20));
        inputOrder.addActionListener(new inputOrderAction());

        addOrder.setIcon(new ImageIcon("images/add.png"));
        editOrder.setIcon(new ImageIcon("images/edit.png"));
        allOrder.setIcon(new ImageIcon("images/all.png"));
        inputOrder.setIcon(new ImageIcon("images/back.png"));
        baseMenu4.add(addOrder);
        baseMenu4.add(editOrder);
        baseMenu4.add(allOrder);
        baseMenu4.add(inputOrder);

        JMenu baseMenu5 = new JMenu("员工");   // 人员管理系统
        baseMenu5.setFont(new Font("宋体", Font.PLAIN, 20));
        baseMenu5.setIcon(new ImageIcon("images/admin.png"));
        JMenuItem addPeo = new JMenuItem("添加新员工");
        addPeo.setFont(new Font("宋体", Font.PLAIN, 20));
        addPeo.addActionListener(new AdminAdd());
        JMenuItem editPeo = new JMenuItem("修改信息");
        editPeo.setFont(new Font("宋体", Font.PLAIN, 20));
        editPeo.addActionListener(new AdminEdit());
        JMenuItem allPeo = new JMenuItem("所有员工");
        allPeo.setFont(new Font("宋体", Font.PLAIN, 20));
        allPeo.addActionListener(new AdminSelect());
        addPeo.setIcon(new ImageIcon("images/add.png"));
        editPeo.setIcon(new ImageIcon("images/edit.png"));
        allPeo.setIcon(new ImageIcon("images/admin1.png"));
        baseMenu5.add(addPeo);
        baseMenu5.add(editPeo);
        baseMenu5.add(allPeo);

        menuBar.add(baseMenu1);   // 加入到菜单栏
        menuBar.add(baseMenu2);
        menuBar.add(baseMenu3);
        menuBar.add(baseMenu4);
        menuBar.add(baseMenu5);

        return menuBar;
    }


    private JToolBar createToolBar() {   //创建工具栏的方法
        /*
         * 主窗体工具栏设计
         * */
        JToolBar toolBar = new JToolBar("工具栏");      //初始化工具栏
        toolBar.setFloatable(false);//设置是否可以移动工具栏

        JButton borrowBookButton = new JButton(new ImageIcon("images/borrow.png"));//图书信息添加按钮
        borrowBookButton.setHideActionText(true);
        borrowBookButton.setToolTipText("借书");
        borrowBookButton.setMargin(new Insets(0, 3, 0, 0));
        borrowBookButton.addActionListener(new borrowBookMen());

        JButton backBookButton = new JButton(new ImageIcon("images/back.png"));//图书信息添加按钮
        backBookButton.setHideActionText(true);
        backBookButton.setToolTipText("还书");
        backBookButton.addActionListener(new backItemAction());
        backBookButton.setMargin(new Insets(0, 10, 0, 2));

        JButton addPeoButton = new JButton(new ImageIcon("images/add.png"));//图书信息添加按钮
        addPeoButton.setHideActionText(true);
        addPeoButton.setToolTipText("添加新读者");
        addPeoButton.addActionListener(new AddNewReader());
        addPeoButton.setMargin(new Insets(0, 2, 0, 0));

        JButton bookAddButton = new JButton(new ImageIcon("images/book.png"));//图书信息添加按钮
        bookAddButton.setHideActionText(true);
        bookAddButton.setToolTipText("添加新图书");
        bookAddButton.setMargin(new Insets(0, 10, 0, 10));
        bookAddButton.addActionListener(new BookAddBut());

        JButton addOrderButton = new JButton(new ImageIcon("images/book1.png"));//图书信息添加按钮
        addOrderButton.setHideActionText(true);
        addOrderButton.setToolTipText("订购图书");
        addOrderButton.addActionListener(new BookOrderBut());


        JButton exitButton = new JButton(new ImageIcon("images/exit.png"));//图书信息添加按钮
        exitButton.setHideActionText(true);
        exitButton.setToolTipText("退出登录");
        exitButton.addActionListener(new ExitBut());

        toolBar.add(borrowBookButton);
        toolBar.add(backBookButton);
        toolBar.addSeparator();
        toolBar.add(addPeoButton);
        toolBar.add(bookAddButton);
        toolBar.add(addOrderButton);
        toolBar.addSeparator();
        toolBar.add(exitButton);

        return toolBar;
    }

    /*private JPanel getJPanel() {
        JPanel panel = new JPanel();
        panel.setBackground(Color.blue);
        JTable table = new JTable(new Object[][]{{"1", "2"}}, new Object[]{"a", "b"});
        panel.add(table);
        return panel;
    }*/

    private class ExitBut implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            new BookLoginIFrame();
            Library.this.setVisible(false);
        }
    }

    private class BookAddBut implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            BookAddIFrame a = new BookAddIFrame();
            addIFrame(a.addBookIFrame);
            Library.this.validate();
            a.addBookIFrame.setVisible(true);
        }
    }

    private class AdminAdd implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            AdminAddFrame a = new AdminAddFrame();
            addIFrame(a);
            Library.this.validate();
            a.setVisible(true);
        }
    }

    private class AdminEdit implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            AdminEditFrame a = new AdminEditFrame();
            addIFrame(a);
            Library.this.validate();
            a.setVisible(true);
        }
    }

    private class AdminSelect implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            AdminSelectFrame a = new AdminSelectFrame();
            addIFrame(a);
            Library.this.validate();
            a.setVisible(true);
        }
    }

    private class BookEditBut implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            BookEditIFrame b = new BookEditIFrame();
            addIFrame(b.editBookIFrame);
            Library.this.validate();
            b.editBookIFrame.setVisible(true);

        }
    }

    private class searchAction implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            ShowBookInfo c = new ShowBookInfo();
            addIFrame(c);
            c.setVisible(true);
        }
    }

    private class BookOrderBut implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            BookOrderIFrame a = new BookOrderIFrame();
            addIFrame(a);
            Library.this.validate();
            a.setVisible(true);
        }
    }

    private class BookOrderEditBut implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            BookOrderEditIFrame b = new BookOrderEditIFrame();
            addIFrame(b);
            Library.this.validate();
            b.setVisible(true);

        }
    }

    private class BookSearchOrderBut implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            BookSearchOrderIFrame b = new BookSearchOrderIFrame();
            addIFrame(b);
            Library.this.validate();
            b.setVisible(true);

        }
    }

    private class borrowBookMen implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            BorrowBook borrowBook = new BorrowBook(operater);
            addIFrame(borrowBook);
            borrowBook.setVisible(true);
        }
    }

    private class backItemAction implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            BackBook backBook = new BackBook(operater);
            addIFrame(backBook);
            backBook.setVisible(true);
        }
    }

    private class setborrowItemAction implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            BorrowSettings borrowSettings = new BorrowSettings();
            addIFrame(borrowSettings);
            borrowSettings.setVisible(true);
        }
    }

    private class xiajiaItemAction implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            OldBook oldBook = new OldBook();
            addIFrame(oldBook);
            oldBook.setVisible(true);
        }
    }

    private class inputOrderAction implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            FromOrderAddBook fromOrderAddBook = new FromOrderAddBook();
            addIFrame(fromOrderAddBook);
            fromOrderAddBook.setVisible(true);
        }
    }

    private class AddNewReader implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            ReaderAddFrame readerAddFrame = new ReaderAddFrame();
            addIFrame(readerAddFrame);
            readerAddFrame.setVisible(true);
        }
    }

    private class EditReaderAction implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            ReaderEditIFrame readerEditIFrame = new ReaderEditIFrame();
            addIFrame(readerEditIFrame);
            readerEditIFrame.setVisible(true);
        }
    }

    private class showReaderAction implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            ShowReadrInfo showReadrInfo = new ShowReadrInfo();
            addIFrame(showReadrInfo);
            showReadrInfo.setVisible(true);
        }
    }
}